package cn.stylefeng.guns.modular.transport.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.DefaultAvatar;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.model.result.OrderTransportResult;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.transport.entity.TransportReceipt;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportReceiptParam;
import cn.stylefeng.guns.modular.transport.model.result.TDispatchResult;
import cn.stylefeng.guns.modular.transport.model.result.TransportReceiptResult;
import cn.stylefeng.guns.modular.transport.service.TDispatchService;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

/**
 * 回传单表控制器
 */
@Controller
@RequestMapping("/transportReceipt")
public class TransportReceiptController extends BaseController {

    private String PREFIX = "/transport/transportReceipt";

    @Autowired
    private TransportReceiptService transportReceiptService;

    @Autowired
    private UserService userService;

    @Autowired
    private FileInfoService  fileInfoService;

	private final ResourceLoader resourceLoader;

    @Autowired
    public TransportReceiptController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/transportReceipt.html";
    }

    @Autowired
    private TDispatchService tDispatchService;

    @Resource
    private OrderTransportService orderTransportService;

    /**
     * 配送设置页面
     */
    @RequestMapping("/toTransportSet")
    public String toTransportSet(TDispatchParam tDispatchParam) {
        return PREFIX + "/transportSet.html";
    }

    /**
     * 配送设置页面
     */
    @RequestMapping("/toCount")
    public String toCount() {
        return PREFIX + "/count.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/transportReceipt_add.html";
    }

    /**
     * 签收页面
     */
    @RequestMapping("/edit_uploads")
    public String edit() {
        return PREFIX + "/transportReceipt_uploads.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TransportReceiptParam transportReceiptParam) {
        this.transportReceiptService.add(transportReceiptParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TransportReceiptParam transportReceiptParam) {
        this.transportReceiptService.update(transportReceiptParam);
        return ResponseData.success();
    }

    /**
     * 修改备货口
     */
    @RequestMapping("/editStockPort")
    @ResponseBody
    public ResponseData editStockPort(TransportReceiptParam transportReceiptParam) {
    	transportReceiptService.update(transportReceiptParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TransportReceiptParam transportReceiptParam) {
        this.transportReceiptService.delete(transportReceiptParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TransportReceiptParam transportReceiptParam) {
        TransportReceipt detail = this.transportReceiptService.getById(transportReceiptParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TransportReceiptParam transportReceiptParam,String condition ) {
        return this.transportReceiptService.findPageBySpec(transportReceiptParam,condition);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list_transportSet")
    public LayuiPageInfo list_transportSet(TransportReceiptParam transportReceiptParam,String condition ) {
        return this.transportReceiptService.findTransportSetPageBySpec(transportReceiptParam,condition);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/findlist_transportSet")
    public ResponseData findlist_transportSet(TransportReceiptParam transportReceiptParam) {
    	List<TransportReceiptResult> list = transportReceiptService.findtransportSetListBySpec(transportReceiptParam);
        return ResponseData.success(list);
    }
    
    /**
     * 显示图片
     * @param fileId
     * @return
     */
    @RequestMapping("/show")
    public ResponseEntity show(String fileId){
        if(fileId!=null&&fileId!=""){
            FileInfo fileInfo = fileInfoService.getById(fileId);
            String fileName=fileInfo.getFileName();
            fileInfo.getFilePath();
            try {
                // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
                //return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName));
                if(fileInfo.getFilePath()!=null&&fileInfo.getFilePath()!=""){
                    if( !FileUtil.exist(fileInfo.getFilePath())){
                        return ResponseEntity.ok(Base64.decode(DefaultAvatar.BASE_64_No));
                    }
                    return ResponseEntity.ok(resourceLoader.getResource("file:" + fileInfo.getFilePath()));
                }else {
                    return ResponseEntity.ok(Base64.decode(DefaultAvatar.BASE_64_No));
                }
            } catch (Exception e) {
                return ResponseEntity.ok(Base64.decode(DefaultAvatar.BASE_64_No));
            }
        }else {
            return ResponseEntity.ok(Base64.decode(DefaultAvatar.BASE_64_No));
        }
    }

}


